package org.cakefoundation.flexcake.business {
	
import flash.net.Responder;
import mx.messaging.messages.RemotingMessage;
import mx.rpc.AsyncToken;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;

/**
 * 	The Responder class provides an object that is used in 
 * 	NetConnection.call() to handle return values from the server.
 */
dynamic public class CakeResponder extends Responder implements ICakeResponder{

	//--------------------------------------------------------------------------
	//
	//  Properties
	//
	//--------------------------------------------------------------------------

	/**
	 * 	@private
	 */		
	private var asyncToken:AsyncToken;
	
	/**
	 * 	@private
	 */		
	private var message:RemotingMessage;

	/**
	 * 	@private
	 */			
	private var resultFunction:Function;

	/**
	 * 	@private
	 */			
	private var statusFunction:Function;

	//--------------------------------------------------------------------------
	//
	//  Constructor
	//
	//--------------------------------------------------------------------------
	
	/**
	 *	Constructs an instance of the responder object with the specified methodName, result function, and status function.
	 */
	public function CakeResponder( methodName:String, result:Function , status:Function  ) {
		super( onQueryResult, onQueryFault );
		resultFunction = (result)? result:notset;
		statusFunction = (status)? status:notset;
		message = new RemotingMessage();
		message.body = "success";	
		message.operation = methodName;
		asyncToken = new AsyncToken( message );
		Alert.show('CakeResponder made '+String((result)? result:'null'));
	}
	
	/**
	 * 	@private
	 */	
    private function onQueryResult( result : Object ):void {
    	message.body = "success";
		resultFunction( String( asyncToken.message.messageId ), result );

    }
	
	/**
	 * 	@private
	 */
    private function onQueryFault( fault : Object ):void {
    	message.body = "fault";
		statusFunction( fault );
    }
    
    /**
     * @private
     */
    private function notset( id:String,result:*):void {
    	Alert.show(id+': you need to set the responder methods here');
    }
    
    /**
     * 	Returns the reference to the AsyncToken for this Responder.
     */
    public function getAsyncToken():AsyncToken {
    	return asyncToken;	
    }
    
	public function result(data:Object):void{ };
	public function fault(info:Object):void{};
	
	
	public function error( data:Object ):void {
	
	}

}

}